package com.training.java.core.collections.car.pre15;

/**
 * The purpose of this class is to demonstrate how equals and hashcode can be
 * overridden to check the objects equality.
 * 
 * @author Satya
 * 
 */
public class CellPhone {

	private String company;
	private String model;
	private String description;
	private String operatingSystem;
	private int price;

	public CellPhone(String company, String model, String operatingSystem,
			int price) {
		this.company = company;
		this.model = model;
		this.operatingSystem = operatingSystem;
		this.price = price;
	}

	// Getters and setters

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getOperatingSystem() {
		return operatingSystem;
	}

	public void setOperatingSystem(String operatingSystem) {
		this.operatingSystem = operatingSystem;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	/**
	 * Here the requirement is
	 * "company, model and operatingSstem together define equality" finding the
	 * equality by overriding the equals and hashCode
	 */

	@Override
	public boolean equals(Object obj) {

		// checking the parameter is real time object or not.
		// if it is not real object, it should
		// return False.

		if (obj instanceof CellPhone)

		{
			CellPhone cellPhone1 = (CellPhone) obj; // Casting
			return this.company.equals(cellPhone1.company)
					&& this.model.equals(cellPhone1.model)
					&& this.operatingSystem.equals(cellPhone1.operatingSystem);

		} else {
			return false;

		}

	}// end of equals method

	// Overriding the hashCode

	@Override
	public int hashCode() {
		return company.hashCode() + model.hashCode()
				+ operatingSystem.hashCode();
	}

}
